﻿using AutoMapper;
using Modules;
using Modules.Corp;
using Modules.Project;
using PmSoft;
using System.ComponentModel.DataAnnotations;

namespace ManagementApi.Models.Device
{
    public class AppCheckinAuthEditForm
    {
        /// <summary>
        /// 主键 
        ///</summary>
        [Display(Name = "主键")]
        public int? Id { get; set; }
        /// <summary>
        /// 项目id 
        ///</summary>
        [Display(Name = "项目id")]
        [Required(ErrorMessage = "{0}不能为空")]
        public int ProjectId { get; set; }
        /// <summary>
        /// 参建单位id 
        ///</summary>
        [Display(Name = "参建单位id")]
        public int? CorpId { get; set; }
        /// <summary>
        /// 开始日期 
        ///</summary>
        [Display(Name = "开始日期")]
        [Required(ErrorMessage = "{0}不能为空")]
        public DateTime StartDate { get; set; }
        /// <summary>
        /// 结束日期 
        ///</summary>
        [Display(Name = "结束日期")]
        [Required(ErrorMessage = "{0}不能为空")]
        public DateTime EndDate { get; set; }
        /// <summary>
        /// 是否有效 
        ///</summary>
        [Display(Name = "是否有效")]
        public bool? IsDisable { get; set; }

        #region 扩展字段
        public string? ProjectName {  get; set; }
        public string? CorpName {  get; set; }
        #endregion
    }

    public static partial class AppCheckinAuthExceptions
    {

        public static AppCheckinAuthEditForm AsForm(this AppCheckinAuth entity)
        {
            AppCheckinAuthEditForm  checkinAuthEditForm= new ();
            var mapper = ServiceAccessor.GetService<IMapper>();
            mapper.Map(entity, checkinAuthEditForm);

            var corpService = ServiceAccessor.GetService<CorpService>();
            checkinAuthEditForm.CorpName = corpService.GetCorpInfo(entity.CorpId ?? 0)?.Name ?? string.Empty;
            var projectService = ServiceAccessor.GetService<ProjectService>();
            checkinAuthEditForm.ProjectName = projectService.GetProjectInfo(entity.ProjectId)?.Name ?? string.Empty;
            return checkinAuthEditForm;
        }
    }
}
